# -*- coding: utf-8 -*-
# standard
import os
import sys
from urllib.parse import quote_plus
from datetime import timedelta, timezone

# third
from celery import Celery


# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)


"""
@Title:  celery 的配置 
@File: celery_conf.py
@Author: walle 2024年12日29日 06时55分20秒
@Version: 1.0.0
@Desc: 
异步调用函数的文档
https://docs.celeryq.dev/en/stable/reference/celery.app.task.html#celery.app.task.Task.apply_async
"""


MQ_SERVER_HOST = "mq.ihcc.com.cn"
MQ_SERVER_PORT = 18104
REDIS_SERVER_HOST = "61.169.129.194"
REDIS_SERVER_PORT = 4999
REDIS_PW = '8gOfXZqXGP2uczte4hRpKw4eMEhqIu7Id8ou416OtV8'
MQ_USER_NAME = "developer"
MQ_PASSWORD = "developer123"
V_HOST = ""
PG_DB_HOST = "pgm-uf69o3rbz151899ngo.pg.rds.aliyuncs.com"
PG_DB_USER = "yhjk"
PG_DB_PW = "Yhjk@240914"
TIMEZONE = "Asia/Shanghai"
TIMEZONE = timezone(timedelta(hours=8))
BROKER_URL = f'amqp://{MQ_USER_NAME}:{quote_plus(MQ_PASSWORD)}@{MQ_SERVER_HOST}:{MQ_SERVER_PORT}/{V_HOST}?heartbeat=600'
# BROKER_URL = f'redis://:{quote_plus(REDIS_PW)}@{REDIS_SERVER_HOST}:{REDIS_SERVER_PORT}/15'
BACKEND_URL = f"db+postgresql://{PG_DB_USER}:{quote_plus(PG_DB_PW)}@{PG_DB_HOST}/task-center"
CELERY_APP = Celery('my_tasks', 
                    broker=BROKER_URL,
                    broker_connection_retry_on_startup=True,  # 启动时重试与broker的连接
                    broker_pool_limit=100,  # broker连接池大小
                    broker_heartbeat=60,  # broker心跳
                    broker_heartbeat_checkrate=2,  # broker心跳检查频率
                    backend=BACKEND_URL,
                    enable_utc=False,
                    # enable_utc=True,
                    timezone="Asia/Shanghai",
                    )
CELERY_APP.conf.database_engine_options = {'echo': True}
CELERY_APP.conf.database_table_names = {
    'task': 'celery_app_task',
    'group': 'celery_app_group',
}
# CELERY_APP.conf.broker_transport_options = {'visibility_timeout': 18000}
CELERY_APP.conf.worker_max_tasks_per_child = 100  # 单个worker 最大处理多少任务就要被新worker 替换
CELERY_APP.conf.worker_max_memory_per_child = 40000  # 40MB  单个worker 最大消耗多少驻留内存



if __name__ == '__main__':
    pass